package com.chuanyang.driverh.utils;

import android.app.ActivityManager;
import android.content.Context;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.utils.push.MyIntentService;
import com.chuanyang.driverh.utils.push.MyPushService;
import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.igexin.sdk.PushManager;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tencent.bugly.crashreport.CrashReport;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.OkHttpClient;


/**
 * 外部sdk的方法的封装
 * @author joyskim
 *
 */
public class SDKUtil {

	/**
	 * umeng SDK会对日志进行加密。加密模式可以有效防止网络攻击，提高数据安全性。
	 */
	public static void encryptUmengData(Context context){
//		AnalyticsConfig.enableEncrypt(true);
	}


	/**
	 * 初始化UIL
	 */
	public static void  initImageLoader(Context context){
		ImageLoader.getInstance().init(new ImageLoaderConfiguration.Builder(context)
				.threadPoolSize(5).build());
	}
	public static void  initIfly(Context context){
		SpeechUtility.createUtility(context, SpeechConstant.APPID +"=58d8f68b");
	}
	public static void  initBugly(Context context){
		// 获取当前包名
		String packageName = context.getPackageName();
// 获取当前进程名
		String processName = JSAppUtil.getProcessName(android.os.Process.myPid());
// 设置是否为上报进程
		CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
		strategy.setUploadProcess(processName == null || processName.equals(packageName));
		CrashReport.initCrashReport(context, "fbb224dc9a", ConstUtil.isDebugMode);//建议在测试阶段建议设置成true，发布时设置为false。 SDK调试模式开关
	}
	public static void  initHttp(Context context){
		//初始化okhttp
		ClearableCookieJar cookieJar1 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context));
		HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
//        CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore());
		OkHttpClient okHttpClient = new OkHttpClient.Builder()
				.connectTimeout(2000L, TimeUnit.MILLISECONDS)
				.readTimeout(8000L, TimeUnit.MILLISECONDS)
				.addInterceptor(new LoggerInterceptor("TAG"))
				.cookieJar(cookieJar1)
				.hostnameVerifier(new HostnameVerifier()
				{
					@Override
					public boolean verify(String hostname, SSLSession session)
					{
						return true;
					}
				})
				.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
				.build();
		OkHttpUtils.initClient(okHttpClient);
	}

	public static void initPush(BaseApplication application) {
		PushManager.getInstance().initialize(application, MyPushService.class);
		PushManager.getInstance().registerPushIntentService(application, MyIntentService.class);


	}
	/**
	 * 用于判断初始化一次sdk
	 * @return
	 */
	public static boolean notProcessAppName(Context context){
		int pid = android.os.Process.myPid();
		String processAppName = getAppName(pid,context);
		// 如果app启用了远程的service，此application:onCreate会被调用2次
		// 为了防止环信SDK被初始化2次，加此判断会保证SDK被初始化1次
		// 默认的app会在以包名为默认的process name下运行，如果查到的process name不是app的process name就立即返回
		//processAppName:com.joyskim.eexpress.client
		//processAppName:com.joyskim.eexpress.client:remote
		return (processAppName == null ||!processAppName.equalsIgnoreCase(context.getPackageName()));
	}


	/**
	 * 获取processAppName
	 * @param pid
	 * @return
	 */
	private static String getAppName(int pid,Context context) {
		String processName = null;
		ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
		List l = am.getRunningAppProcesses();
		Iterator i = l.iterator();
		while (i.hasNext()) {
			ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
			try {
				if (info.pid == pid) {
					processName = info.processName;
					return processName;
				}
			} catch (Exception e) {
			}
		}
		return processName;
	}

}
